<?php

namespace ManiaLivePlugins\MLEPP\Widgets\Widgets\Views\Defaults\MessageWindow;

use ManiaLive\Data\Storage;
use ManiaLivePlugins\MLEPP\Widgets\Structures\TypeView;

class Standard extends TypeView {

    private $frame;
    private $messages = array();
    private $tabs = array();
    private $init = false;

    public function initializeComponents() {
        
    }

    public function setSettings($set) {
        $this->settings = $set;

        if ($this->frame == null) {
            $this->frame = new \ManiaLive\Gui\Windowing\Controls\Tabview();
            $this->clearComponents();
            $this->addComponent($this->frame);
            $this->messages['general'] = array();
            $this->createNewTab('general');
            $this->frame->setSize(125, 60);
        }
    }

    public function onDraw() {

        /* foreach ($this->tabs as $tab) {
          $tab->updateTab();
          } */
    }

    public function setData($d) {
        if ($d == null)
            return;

        $login = $d['fromLogin'];
        $toLogin = $d['toLogin'];
        if (empty($d['tab']))
            $d['tab'] = 'general';
        //print "$login, $toLogin, ".$this->getRecipient();
        if ($toLogin == false | $toLogin == $this->getRecipient()) {
            $mlepp = \ManiaLivePlugins\MLEPP\Core\Mlepp::getInstance();
            $text = $mlepp->parseColors($d['message']);

            if (empty($this->messages[$d['tab']])) {
                $this->messages[$d['tab']] = array();
                $this->createNewTab($d['tab']);
            }

            array_push($this->messages[$d['tab']], array("login" => $login, "message" => $text));

            if (count($this->messages[$d['tab']]) > 7) {
                $this->messages[$d['tab']] = array_slice($this->messages[$d['tab']], (count($this->messages[$d['tab']]) - 7), 7);
            }
            $text2 = "";
            foreach ($this->messages[$d['tab']] as $message) {
                $text2 .= '$z$s' . (string) $message['message'] . "\n";
            }
			
            $this->tabs[$d['tab']]->setText($text2);
            $this->tabs[$d['tab']]->setTitle('$f00' . $d['tab']);
        }
    }

    private function createNewTab($tabName) {
        if (!isset($this->tabs[$tabName])) {
            $this->tabs[$tabName] = new \ManiaLivePlugins\MLEPP\Widgets\Widgets\VHelpers\DynamicTab();
            $this->tabs[$tabName]->setTitle($tabName);
            $this->frame->addTab($this->tabs[$tabName]);
        }
    }

    public function getWidgetSizeX() {
        return ($this->settings->width);
    }

    public function getWidgetSizeY() {
        return ($this->settings->height);
    }

    public function destroy() {
        parent::destroy();
    }

}

?>
